﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/24 9:35:26
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/


using System;
using System.Collections.Generic;
using System.IO;

namespace CommonLibrary
{
    /// <summary>
    /// 产品数据显示代理事件
    /// </summary>
    /// <param name="lis"></param>
    public delegate void AddProductDataToGridDelegate(List<ProductInfo> lis);
    /// <summary>
    /// 生产记录加载与保存管理类
    /// </summary>
    public class ProductRecordManager
    {
        /// <summary>
        /// 产品数据显示事件
        /// </summary>
        public static AddProductDataToGridDelegate AddProductDataToGrid = null;
        /// <summary>
        /// 定义接口
        /// </summary>
        private static readonly Lazy<ProductRecordManager> lazy = new Lazy<ProductRecordManager>(() => new ProductRecordManager());
        /// <summary>
        /// 类入口
        /// </summary>
        public static ProductRecordManager Instance { get { return lazy.Value; } }

        /// <summary>
        /// 构造函数
        /// </summary>
        public ProductRecordManager()
        {
        }
        /// <summary>
        /// 初始化
        /// </summary>
        public void Init()
        {
            ReadOldRecord();
        }
        /// <summary>
        /// 读取旧记录
        /// </summary>
        public void ReadOldRecord()
        {
            LoadLocalHeliumRecord();
        }
        private void LoadLocalHeliumRecord()
        {
            var list = ProductSQLiteDb.GetTodayProductInfoList();
            if (list != null && list.Count > 0 && AddProductDataToGrid!=null)
            {
                AddProductDataToGrid(list);
            }
        }
        #region 增加记录
        private object ADD_HELIUM_LOCK = new object();
        /// <summary>
        /// 增加产品数据
        /// </summary>
        /// <param name="pi"></param>
        public void AddHeliumData(ProductInfo pi)
        {
            if (pi != null)
            {
                ProductSQLiteDb.AddProductInfo(pi);
                pi.Format();
                if (!ProductFileManager.IsProductIntervalSave)
                {
                    lock (ADD_HELIUM_LOCK)
                    {
                        CommonHelper.WriteExcel(GetExcelFileName($"{ProductFileManager.FILE_PREV_NAME}_"), new List<ProductInfo>() { pi });
                    }
                }
                AddProductDataToGrid(new List<ProductInfo>() { pi });
            }
        }
        #endregion
        /// <summary>
        /// 获取产品数据文件名称
        /// </summary>
        /// <param name="prevName"></param>
        /// <returns></returns>
        public string GetExcelFileName(string prevName="")
        {
            DateTime now = DateTime.Now;
            TimeSpan time = now.TimeOfDay;
            TimeSpan startTime = TimeSpan.Parse("08:00:00");
            TimeSpan endTime = TimeSpan.Parse("20:00:00");
            string tag = time.Subtract(startTime).TotalSeconds >= 0 && endTime.Subtract(time).TotalSeconds >= 0 ? "白班" : "晚班";
            return Path.Combine(Path.Combine(CommonHelper.GetBaseDataDirectory(), now.ToString("yyyy-MM-dd")), $"{prevName}{AppBasicSetting.Instance().EquipmentCode}_{now.ToString("yyyyMMdd")}_{tag}.xlsx");
        }
    }
}
